package com.ivo.biz;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.ivo.core.WebConstants;
import com.ivo.quartz.CellPerformanceQuartz;
import com.ivo.sys.user.User;
import com.ivo.util.JsonUtil;

/**
 * 
 *
 * @author Minkey Wang
 * @version 2017年6月1日
 */
@RestController
@RequestMapping("/cell/performance")
public class CellPerformanceController {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private PerformanceService performanceService;
    
    @Autowired
    private CellPerformanceQuartz cellPerformanceQuartz;
	
	@RequestMapping(value = "/location")
	@ResponseBody
	public ModelAndView location(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/cel");
		mv.addObject("category","cellPerformance");
		return mv;
		
	}
	
	@RequestMapping(value = "/dustfree")
	@ResponseBody
	public ModelAndView dustfree(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/cel");
		mv.addObject("category","dustfree");
		return mv;
		
	}
	
	@RequestMapping(value = "/majorAddition")
	@ResponseBody
	public ModelAndView majorAddition(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/cel");
		mv.addObject("category","majorAddition");
		return mv;
		
	}
	
	@RequestMapping(value = "/maintainByMon")
	@ResponseBody
	public ModelAndView maintainByMon(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/cel");
		mv.addObject("category","maintainByMon");
		return mv;
		
	}
	
	@RequestMapping(value = "/maintainByDay")
	@ResponseBody
	public ModelAndView maintainByDay(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/cel");
		mv.addObject("category","maintainByDay");
		return mv;
		
	}
	@RequestMapping(value = "/locationAudit")
	@ResponseBody
	public ModelAndView locationAudit(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/cel");
		mv.addObject("category","locationAudit");
		return mv;
		
	}
	
    @RequestMapping(value = "/list")
    @ResponseBody
    public String list(
    		@RequestParam(value = "allFlag", required = false) int allFlag,
    		@RequestParam(value = "pno", required = false) int pno,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "bonus", required = false) Double bonus,
    		@RequestParam(value = "score", required = false) Double score,
    		@RequestParam(value = "ope_fk[]", required = false) Long[] ope_fk,
    		@RequestParam(value = "classType", required = false) String classType,
    		@RequestParam(value = "condition", required = false) String condition) {
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.list(allFlag,pno,emp_id,month,bonus,score,ope_fk,classType,condition));
        return JsonUtil.toJson(returnMap);
    }
    
    
    @RequestMapping(value = "/getAverageBouns")
    @ResponseBody
    public String getAverageBouns(
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "ope_fk[]", required = false) Long[] ope_fk,
    		@RequestParam(value = "classType", required = false) String classType
    		) {
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.getAverageBouns(month,ope_fk,classType));
        String s =  JsonUtil.toJson(returnMap);
        return JsonUtil.toJson(returnMap);
    }
    
    @RequestMapping(value = "/listmaintainByMon")
    @ResponseBody
    public String listmaintainByMon(
    		HttpServletRequest request,
    		@RequestParam(value = "allFlag", required = false) int allFlag,
    		@RequestParam(value = "pno", required = false) int pno,
    		@RequestParam(value = "factory", required = false) String factory,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "emp_name", required = false) String emp_name,
    		@RequestParam(value = "condition", required = false) String condition,
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "ope_fk", required = false) Long ope_fk,
    		@RequestParam(value = "classType", required = false) String classType
    		){
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.listmaintainByMon(allFlag,pno,factory, emp_id, emp_name,condition,month,ope_fk,classType));
        return JsonUtil.toJson(returnMap);
    }
    
    @RequestMapping(value = "/listmaintainByDay")
    @ResponseBody
    public String listmaintainday(
    		HttpServletRequest request,
    		@RequestParam(value = "allFlag", required = false) int allFlag,
    		@RequestParam(value = "pno", required = false) int pno,
    		@RequestParam(value = "factory", required = false) String factory,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "emp_name", required = false) String emp_name,
    		@RequestParam(value = "from_date", required = false) String from_date,
    		@RequestParam(value = "to_date", required = false) String to_date,
    		@RequestParam(value = "ope_id", required = false) Long ope_id,
    		@RequestParam(value = "type", required = false) Long type,
    		@RequestParam(value = "auditType", required = false) Long auditType,
    		@RequestParam(value = "condition", required = false) String condition) throws ParseException{
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.listmaintainByDay(allFlag,pno,factory, emp_id, emp_name,from_date,to_date,ope_id,type,auditType,condition));
//        String s =  JsonUtil.toJson(returnMap);
        return JsonUtil.toJson(returnMap);
    }
    
    
    
    @RequestMapping(value = "/listdustfree")
    @ResponseBody
    public String listdustfree(
    		HttpServletRequest request,
    		@RequestParam(value = "allFlag", required = false) int allFlag,
    		@RequestParam(value = "pno", required = false) int pno,
    		@RequestParam(value = "factory", required = false) String factory,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "ope_fk[]", required = false) Long[] ope_fk,
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "classType", required = false) String classType,
    		@RequestParam(value = "condition", required = false) String condition) throws ParseException{
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.listdustfree(allFlag,pno,factory, emp_id,ope_fk,classType,month,condition));
//        String s =  JsonUtil.toJson(returnMap);
        return JsonUtil.toJson(returnMap);
    }
    
    
    @RequestMapping(value = "/listAudit")
    @ResponseBody
    public String listAudit(
    		HttpServletRequest request,
    		@RequestParam(value = "allFlag", required = false) int allFlag,
    		@RequestParam(value = "pno", required = false) int pno,
    		@RequestParam(value = "factory", required = false) String factory,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "emp_name", required = false) String emp_name,
    		@RequestParam(value = "from_date", required = false) String from_date,
    		@RequestParam(value = "to_date", required = false) String to_date,
    		@RequestParam(value = "ope_id", required = false) Long ope_id,
    		@RequestParam(value = "type", required = false) Long type,
    		@RequestParam(value = "auditType", required = false) Long auditType,
    		@RequestParam(value = "condition", required = false) String condition) throws ParseException{
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.listAudit(allFlag,pno,factory, emp_id, emp_name,from_date,to_date,ope_id,type,auditType,condition));
        return JsonUtil.toJson(returnMap);
    }
    
    @RequestMapping(value = "/listMajorAddition")
    @ResponseBody
    public String listMajorAddition(
    		HttpServletRequest request,
    		@RequestParam(value = "allFlag", required = false) int allFlag,
    		@RequestParam(value = "pno", required = false) int pno,
    		@RequestParam(value = "factory", required = false) String factory,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "ope_fk[]", required = false) Long[] ope_fk,
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "amount", required = false) Double amount,
    		@RequestParam(value = "classType", required = false) String classType,
    		@RequestParam(value = "condition", required = false) String condition) throws ParseException{
	    	Map<String, Object> returnMap = new HashMap<String, Object>();
	    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.listMajorAddition(allFlag,pno,factory, emp_id,ope_fk,amount,classType,month,condition));
//        String s =  JsonUtil.toJson(returnMap);
        return JsonUtil.toJson(returnMap);
    }
    @RequestMapping(value = "/updateMajorAmount", method = RequestMethod.POST)
    @ResponseBody
    public String updateMajorAmount(HttpServletRequest request,
    		@RequestParam(value = "month") String month,
    		@RequestParam(value = "emp_id") String emp_id,
    		@RequestParam(value = "amount") Double amount) {
    	String err_code = "";
    	String err_msg = "";
    	String result = performanceService.updateMajorAmount(month,emp_id,amount);
    	if(result.equals("1")){
    		err_code = "1";
    		err_msg = "员工"+emp_id+"不存在！";
    	}else if(result.equals("0")){
    		err_code = "0";
    		err_msg = "success";
    	}
    	
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", err_code);
        returnMap.put("err_msg", err_msg);
        return JsonUtil.toJson(returnMap);
    }
    @RequestMapping(value = "/updateDustfree", method = RequestMethod.POST)
    @ResponseBody
    public String updateDustfree(HttpServletRequest request,
    		@RequestParam(value = "month") String month,
    		@RequestParam(value = "emp_id") String emp_id,
    		@RequestParam(value = "amount") Double amount) {
    	String err_code = "";
    	String err_msg = "";
    	String result = performanceService.updateDustfree(month,emp_id,amount);
    	if(result.equals("1")){
    		err_code = "1";
    		err_msg = "员工"+emp_id+"不存在！";
    	}else if(result.equals("0")){
    		err_code = "0";
    		err_msg = "success";
    	}
    	
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", err_code);
        returnMap.put("err_msg", err_msg);
        return JsonUtil.toJson(returnMap);
    } 
    @RequestMapping(value = "/updateByMon", method = RequestMethod.POST)
    @ResponseBody
    public String updateByMon(HttpServletRequest request,
    		@RequestParam(value = "month") String month,
    		@RequestParam(value = "emp_id") String emp_id,
    		@RequestParam(value = "discipline") String discipline,
            @RequestParam(value = "compatibility") String compatibility) {
    	String err_code = "";
    	String err_msg = "";
    	String user_id ="";
    	User user = (User) request.getSession().getAttribute(WebConstants.SessionUser);
    	if(user!=null) user_id = user.getUser_id();
    	String result = performanceService.updateByMon(month,emp_id,discipline,compatibility,user_id);
    	if(result.equals("1")){
    		err_code = "1";
    		err_msg = "员工"+emp_id+"不存在！";
    	}else if(result.equals("0")){
    		err_code = "0";
    		err_msg = "success";
    	}
    	
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", err_code);
        returnMap.put("err_msg", err_msg);
        return JsonUtil.toJson(returnMap);
    }
    
    @RequestMapping(value = "/createByDay", method = RequestMethod.POST)
    @ResponseBody
    public String createByDay(HttpServletRequest request,
    		@RequestParam(value = "factory") String factory,
            @RequestParam(value = "category") String category,
            @RequestParam(value = "fabDate") String fabDate,
            @RequestParam(value = "type") Long type,
            @RequestParam(value = "ope_id") Long ope_id,
            @RequestParam(value = "emp_id") String emp_id,
            @RequestParam(value = "emp_name") String emp_name,
            @RequestParam(value = "group_leader") String group_leader,
            @RequestParam(value = "reason") String reason,
            @RequestParam(value = "class_type") String class_type
    		) throws ParseException {
    	
    	String err_code = "";
    	String err_msg = "";
    	String data = performanceService.createByDay(factory,category,fabDate,type,ope_id,emp_id,emp_name,group_leader,class_type,reason);
    	if(!data.equals("0")){
    		err_code = "0";
    		err_msg = "success";
    	}
    	else {
    		err_code = "1";
    		err_msg = "create failed";
    	}
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", err_code);
        returnMap.put("err_msg", err_msg);
        return JsonUtil.toJson(returnMap);
    }
    @RequestMapping(value = "/deleteByDay")
    public String delete(@RequestParam(value = "id") Long id) {
    	String err_code = "";
    	String err_msg = "";
    	int r = performanceService.deleteByDay(id);
    	if(r==1){
    		err_code = "0";
    		err_msg = "success";
    	}
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", err_code);
        returnMap.put("err_msg", err_msg);
        return JsonUtil.toJson(returnMap);
    }
    @RequestMapping(value = "/syschron", method = RequestMethod.POST)
    @ResponseBody
    public String syschron(HttpServletRequest request) {
    	cellPerformanceQuartz.execute();
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        return JsonUtil.toJson(returnMap);
    }
}
